package com.sphd.restaurantsystem.activitys.vip;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SearchView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.VipAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.info.VipInfo;
import com.sphd.restaurantsystem.presenter.VipPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.VipView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
*
* 会员搜索界面
* */
public class SearchVipActivity extends BaseActivity implements VipView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.search_view)
    SearchView searchView;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.content_search_vip)
    LinearLayout contentSearchVip;
    VipAdapter vipAdapter;
    VipPresenterImpl vipPresenter;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    VipInfo vipInfo;
    @BindView(R.id.search_btn)
    Button searchBtn;
    InputMethodManager inputMethodManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_vip);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (!TextUtils.isEmpty(query)) {
                    addData(query);
                } else {
                    toast("请输入会员卡号后点击搜索");
                }
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                bundle.putSerializable(Constant.VIPINFO, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context, VipInfomationActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemLongClick(adapter, view, position);
                showDeleVipDialog(position);

            }
        });

    }

    /**
     * 弹出来删除会员的提示框
     */
    private void showDeleVipDialog(final int positon) {
        vipInfo = vipAdapter.getItem(positon);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        // set dialog message
        alertDialogBuilder
                .setTitle("删除提示")
                .setMessage("你将删除" + ((VipInfo) (vipAdapter.getItem(positon))).getMem_name() + "会员?")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                vipPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.DelVip
                                        + "id=" + vipInfo.getId());
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }


    @Override
    public void recDelRes(int res) {
        if (res == 1) {
            bundle.putInt(Constant.TYPE, vipInfo.getMem_grade());
            intent.putExtras(bundle);
            setResult(2, intent);
            finish();
        } else {
            toast("删除失败，请重试！");
        }
    }

    private void initView() {
        setTitleBar(toolbar, "会员查找");
        inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        vipPresenter = new VipPresenterImpl(this);
        initAdapter();

    }

    private void addData(String query) {
        vipPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.SearchVip + "cgid=" + restaurantInfo.getId() + "&card=" + query);
    }

    private void initAdapter() {
        setRecy(myRecy);
        vipAdapter = new VipAdapter(R.layout.item_vip, new ArrayList<>());
        myRecy.setAdapter(vipAdapter);
    }

    @Override
    public void recRes(int com, int dis, int topup,int topupno) {

    }

    @Override
    public void recList(List<VipInfo> vipInfos) {
        if (vipInfos.size() == 0) {
            vipAdapter.setNewData(vipInfos);
            toast("暂无该卡号人员信息");
        } else {
            hideSoftInput();
            vipAdapter.setNewData(vipInfos);

        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }

    @OnClick(R.id.search_btn)
    public void onClick() {
        if (!TextUtils.isEmpty(searchView.getQuery().toString())) {
            addData(searchView.getQuery().toString());
        } else {
            toast("请输入会员卡号后点击搜索");
        }
    }

    private void hideSoftInput() {
        if (inputMethodManager != null) {
            View v = this.getCurrentFocus();
            if (v == null) {
                return;
            }

            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),
                    0);
//            searchView.clearFocus();
        }
    }
}
